package com.threeonemeeting.activity;

import java.util.Map;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;

import com.snda.lib.http.HttpUtil;
import com.snda.lib.http.ICallBack;
import com.snda.lib.http.TaskManager;
import com.snda.lib.ui.MessageBox;
import com.snda.lib.util.MiscHelper;
import com.threeonemeeting.Const;
import com.threeonemeeting.DataCenter;
import com.threeonemeeting.R;
import com.threeonemeeting.model.UserInfo;
import com.threeonemeeting.tab.BaseTabActivity;
import com.threeonemeeting.task.HttpConst;
import com.threeonemeeting.task.SendFeedbackTask;

public class FeedBackActivity extends BaseTabActivity implements ICallBack {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_feedback);
		init("意见反馈");

		UserInfo currentUserInfo = DataCenter.getInstance().currentLoginUserInfo;

		// 帐号编辑框
		final EditText editAccount = (EditText) findViewById(R.id.editAccount);
		editAccount.setOnFocusChangeListener(new OnFocusChangeListener() {

			public void onFocusChange(View v, boolean hasFocus) {
				String strHint = getResources().getString(
						R.string.string_input_name);
				String strAccount = editAccount.getText().toString();
				if (hasFocus == true) {
					if (strAccount.equalsIgnoreCase(strHint) == true) {
						editAccount.setText("");
					}
				} else {

					if (strAccount.equalsIgnoreCase("") == true) {
						editAccount.setText(strHint);
					}
				}
			}
		});

		// 电话编辑框
		final EditText editMobile = (EditText) findViewById(R.id.editMobile);
		editMobile.setOnFocusChangeListener(new OnFocusChangeListener() {

			public void onFocusChange(View v, boolean hasFocus) {
				String strHint = getResources().getString(
						R.string.string_input_tel);
				String strMobile = editMobile.getText().toString();
				if (hasFocus == true) {
					if (strMobile.equalsIgnoreCase(strHint) == true) {
						editMobile.setText("");
					}
				} else {

					if (strMobile.equalsIgnoreCase("") == true) {
						editMobile.setText(strHint);
					}
				}
			}
		});

		// Email
		final EditText editEmail = (EditText) findViewById(R.id.editEmail);
		editEmail.setOnFocusChangeListener(new OnFocusChangeListener() {

			public void onFocusChange(View v, boolean hasFocus) {
				String strHint = getResources().getString(
						R.string.string_input_email);
				String strEmail = editEmail.getText().toString();
				if (hasFocus == true) {
					if (strEmail.equalsIgnoreCase(strHint) == true) {
						editEmail.setText("");
					}
				} else {

					if (strEmail.equalsIgnoreCase("") == true) {
						editEmail.setText(strHint);
					}
				}
			}
		});

		if (currentUserInfo.nUserId != 0) {
			if (MiscHelper.isEmpty(currentUserInfo.strRealName) == false) {
				editAccount.setText(currentUserInfo.strRealName);
			}

			if (MiscHelper.isEmpty(currentUserInfo.strMobile) == false) {
				editMobile.setText(currentUserInfo.strMobile);
			}

			if (MiscHelper.isEmpty(currentUserInfo.strEmail) == false) {
				editEmail.setText(currentUserInfo.strEmail);
			}
		}

		// FeedBack
		final EditText editFeedBack = (EditText) findViewById(R.id.editFeedback);
		editFeedBack.setOnFocusChangeListener(new OnFocusChangeListener() {

			public void onFocusChange(View v, boolean hasFocus) {
				String strHint = getResources().getString(
						R.string.string_input_feedback);
				String strFeedBack = editFeedBack.getText().toString();
				if (hasFocus == true) {
					if (strFeedBack.equalsIgnoreCase(strHint) == true) {
						editFeedBack.setText("");
					}
				} else {

					if (strFeedBack.equalsIgnoreCase("") == true) {
						editFeedBack.setText(strHint);
					}
				}
			}
		});

		// 反馈按钮
		Button btnCommit = (Button) findViewById(R.id.btn_submit);
		OnClickListener commitBtnClickListener = new OnClickListener() {

			public void onClick(View v) {
				// 姓名
				String strHint = getResources().getString(
						R.string.string_input_name);
				if (editAccount.getText().toString().equalsIgnoreCase("") == true
						|| editAccount.getText().toString()
								.equalsIgnoreCase(strHint) == true) {

					editAccount.setFocusable(true);
					editAccount.setFocusableInTouchMode(true);
					editAccount.requestFocus();

					MessageBox.showToast(FeedBackActivity.this, "姓名必须填写");
					return;
				}

				// 电话
				strHint = getResources().getString(R.string.string_input_tel);
				if (editMobile.getText().toString().equalsIgnoreCase("") == true
						|| editMobile.getText().toString()
								.equalsIgnoreCase(strHint) == true) {

					editMobile.setFocusable(true);
					editMobile.setFocusableInTouchMode(true);
					editMobile.requestFocus();

					MessageBox.showToast(FeedBackActivity.this, "电话必须填写");
					return;
				}

				// Email
				strHint = getResources().getString(R.string.string_input_email);
				String strEmail = editEmail.getText().toString();
				if (editEmail.getText().toString().equalsIgnoreCase("") == true
						|| editEmail.getText().toString()
								.equalsIgnoreCase(strHint) == true) {
					editEmail.setFocusable(true);
					editEmail.setFocusableInTouchMode(true);
					editEmail.requestFocus();

					MessageBox.showToast(FeedBackActivity.this, "Email必须填写");
					return;
				}

				if (MiscHelper.isEmail(strEmail) == false) {
					MessageBox.showToast(
							FeedBackActivity.this.getApplicationContext(),
							"请输入有效的Email地址");

					editEmail.setFocusable(true);
					editEmail.setFocusableInTouchMode(true);
					editEmail.requestFocus();
					return;
				}

				// 意见
				strHint = getResources().getString(
						R.string.string_input_feedback);
				if (editFeedBack.getText().toString().equalsIgnoreCase("") == true
						|| editFeedBack.getText().toString()
								.equalsIgnoreCase(strHint) == true) {
					MessageBox.showToast(FeedBackActivity.this, "写点意见哦");

					editFeedBack.setFocusable(true);
					editFeedBack.setFocusableInTouchMode(true);
					editFeedBack.requestFocus();

					return;
				}

				// 发送响应请求
				SendFeedbackTask task = new SendFeedbackTask(
						FeedBackActivity.this, FeedBackActivity.this);
				Bundle params = new Bundle();
				params.putString("RealName", editAccount.getText().toString());
				params.putString("Phone", editMobile.getText().toString());
				params.putString("Email", editEmail.getText().toString());
				params.putString("Summary", editFeedBack.getText().toString());
				TaskManager.addTask(task, params, true);
			}
		};
		if (btnCommit != null) {
			btnCommit.setOnClickListener(commitBtnClickListener);
		}
	}

	public void doCallBack(Map<String, Object> mapData) {
		Integer msgType = (Integer) mapData.get(HttpConst.KEY_TASK_TYPE);
		if (msgType == null) {
			Log.d(Const.Tag, "can't find type in response type");
			return;
		}

		Integer nError = (Integer) mapData.get(HttpUtil.KEY_ERROR_CODE);
		if (nError == null || nError != HttpUtil.ERROR_SUCCESS) {
			Log.d(Const.Tag, "response return error:" + nError + " type:"
					+ msgType);
			return;
		}

		if (msgType != HttpConst.TASKTYPE_SENDFEEDBACK) {
			return;
		}

		Boolean bSuccess = (Boolean) mapData.get("result");
		if (bSuccess == null) {
			MessageBox.showToast(getApplicationContext(), "提交意见反馈失败了，要不您在试试？");
			return;
		}

		MessageBox.showToast(getApplicationContext(),
				"您的意见已经收到，稍候我们会跟您联系，感谢您的支持！");
		startActivity("MainMoreActivity");
	}
}
